package com.spring.shiro.util;

import com.spring.shiro.dao.DeptMapper;
import com.spring.shiro.dao.RoleMapper;
import com.spring.shiro.dao.UserMapper;
import com.spring.shiro.model.Dept;
import com.spring.shiro.model.Role;
import com.spring.shiro.model.ShiroUser;
import com.spring.shiro.model.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class BeanUtil {
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private DeptMapper deptMapper;
	@Autowired
	private RoleMapper roleMapper;


	SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm");


	/**
	 * 将数据库用户类 转化为 封装了更多信息的用户类
	 * 
	 * @param shiroUser
	 * @return
	 */
	public User toUser(ShiroUser shiroUser) {
		User user = new User();
		user.setAvator("0".equals(shiroUser.getAvator()) ? "女" : "男");
		user.setId(shiroUser.getId());
		int deptId = shiroUser.getDeptid();
		Dept dept = deptMapper.findById(deptId).get();
		String deptName = dept.getName();
		user.setDept(deptName);
		user.setPassword(shiroUser.getPassword());
		Role role = roleMapper.findById(shiroUser.getRoleid()).get();
		user.setRole(role.getRoles());
		user.setRoleid(shiroUser.getRoleid());
		user.setRoleTip(role.getTips());
		user.setSalt(shiroUser.getSalt());
		user.setUsername(shiroUser.getUsername());
		return user;
	}

	/**
	 * 用户类 List转化
	 * 
	 * @param userList
	 * @return
	 */
	public List<User> toUserList(List<ShiroUser> userList) {

		List<User> users = new ArrayList<User>();
		for (ShiroUser user : userList) {
			users.add(toUser(user));
		}
		return users;
	}



	
	
}
